package com.dzy.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dzy.base.BaseController;
import com.dzy.entity.Role;
import com.dzy.service.PermissionService;
import com.dzy.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {
    @Reference
    private RoleService roleService;
//    @RequestMapping
//    public String findAll(Model model){
//        List<Role> roleList = this.roleService.findAll();
//        model.addAttribute("list",roleList);
//        return "role/index"; //WEB-INF/templates/role/index.html
//        //return "/role/index"; //WEB-INF/templates//role/index.html
//        //return "role/index.html";
//    }
    @PreAuthorize("hasAuthority('role.show')")
    @RequestMapping
    public String findPage(HttpServletRequest request, Model model) {
        //1.获取和封装查询条件和分页参数
        // key    value
        //roleName:管理
        //pageNum：2
        // pageSize：5
        Map<String, Object> filters = getFilters(request);
        //2.调用业务层获取分页相关的数据和参数
        PageInfo<Role> pageInfo = this.roleService.findPage(filters);
        model.addAttribute("page", pageInfo);//pageNum pageSize |  roleList  navigatepageNums
        model.addAttribute("filters", filters);// pageNum pageSize  |  roleName
        //跳转到页面
        return "role/index"; //WEB-INF/templates/role/index.html
        //return "common/head";
        //return "/role/index"; //WEB-INF/templates//role/index.html
        //return "role/index.html";
    }

    @Reference
    private PermissionService permissionService;



    @PreAuthorize("hasAuthority('role.assgin')")
    @RequestMapping("/assignShow/{id}")
    public String assignShow(@PathVariable("id") Long roleId,Model model) {
        List<Map<String, Object>> zNodes = this.permissionService.findPermissionsByRoleId(roleId);
        model.addAttribute("roleId",roleId);
        model.addAttribute("zNodes",zNodes);
        return "role/assignShow";
    }
    @PreAuthorize("hasAuthority('role.assgin')")
    @RequestMapping("/assignPermission")
    public String assignPermission(Long roleId,Long[] permissionIds){
        this.permissionService.saveRolePermissionRealtionShip(roleId,permissionIds);
        return "common/successPage";
    }
    @PreAuthorize("hasAuthority('role.create')")
    @RequestMapping("/create")
    public String create() {
        return "role/create";
    }
    @PreAuthorize("hasAuthority('role.create')")
    @RequestMapping("/save")
    public String save(Role role) {
        this.roleService.insert(role);
        //return "/role";
        //return "role"; //WEB-INF/templates/role.html
        //return "redirect:/role"; //!!!!
        return "common/successPage";
    }
    @PreAuthorize("hasAuthority('role.edit')")
    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Long id, Model model) {
        Role role = this.roleService.getById(id);
        model.addAttribute("role", role);
        return "role/edit";
    }
    @PreAuthorize("hasAuthority('role.edit')")
    @RequestMapping("/update")
    public String update(Role role) {
        this.roleService.update(role);
        return "common/successPage";
    }
    @PreAuthorize("hasAuthority('role.delete')")
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Long id) {
        this.roleService.delete(id);
        return "redirect:/role";
    }

}
